package com.hspedu.reflection.homework;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author DL5O
 * @version 1.0
 */
public class Homework01 {
    public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, InstantiationException {
        Class<PrivateTest> ptClass = PrivateTest.class;
        Constructor<PrivateTest> ptConstructor = ptClass.getDeclaredConstructor();//得到该类的无参构造器
        PrivateTest privateTest = ptConstructor.newInstance();//进行实例化

        Field name = ptClass.getDeclaredField("name");//得到私有属性name
        name.setAccessible(true);

        //得到该类的getName() 方法
        Method getName = ptClass.getMethod("getName");

        System.out.println("修改前:" + getName.invoke(privateTest));
        name.set(privateTest,"大龙");
        System.out.println("修改后:" + getName.invoke(privateTest));
    }
}

class PrivateTest {
    private String name = "hellokitty";


    public String getName() {
        return name;
    }
}
